using System;
using System.Collections;
using UnityEngine;

public class SubscriptionButtonOLD : MonoBehaviour
{
	public UILabel timer;

	public UILabel infoText;

	public UITexture texture;

	public GameObject holder;

	private bool canTakeReward;

	private void Awake()
	{
		InAppSubscriptionsController.OnStartSubscription += StartSubscriptionEventHandler;
		InAppSubscriptionsController.OnEndSubscription += EndSubscriptionEventHandler;
		InAppSubscriptionsController.OnStartTimer += StartTimerEventHandler;
		InAppSubscriptionsController.OnJSONLoaded += JSONLoadedEventHandler;
		Init();
	}

	private void OnDestroy()
	{
		InAppSubscriptionsController.OnStartSubscription -= StartSubscriptionEventHandler;
		InAppSubscriptionsController.OnEndSubscription -= EndSubscriptionEventHandler;
		InAppSubscriptionsController.OnStartTimer -= StartTimerEventHandler;
		InAppSubscriptionsController.OnJSONLoaded -= JSONLoadedEventHandler;
	}

	private void OnClick()
	{
		if (canTakeReward)
		{
			canTakeReward = !InAppSubscriptionsController.instance.TryTakeReward();
		}
	}

	private void StartSubscriptionEventHandler()
	{
	}

	private void EndSubscriptionEventHandler()
	{
		SwitchHolder(false);
	}

	private void StartTimerEventHandler()
	{
		StartTimer();
		ShowSubscriptionsTimer();
		SwitchHolder(true);
	}

	private void JSONLoadedEventHandler()
	{
		Init();
	}

	private void Init()
	{
		if (InAppSubscriptionsController.instance.GetSubscriptionState())
		{
			StartTimer();
			ShowSubscriptionsTimer();
			SwitchHolder(true);
		}
		else
		{
			SwitchHolder(false);
		}
	}

	private void SwitchHolder(bool val)
	{
		holder.SetActive(val);
	}

	private void StartTimer()
	{
		StartCoroutine(TimerIEnumerator(InAppSubscriptionsController.instance.GetCycleRemaininTime()));
	}

	private void ShowSubscriptionsTimer()
	{
		infoText.text = InAppSubscriptionsController.instance.GetSubscriptionRemaininDays().ToString();
	}

	private IEnumerator TimerIEnumerator(int secondsToOpen)
	{
		for (int currentTimer = secondsToOpen; currentTimer > 0; currentTimer--)
		{
			TimeSpan t = TimeSpan.FromSeconds(currentTimer);
			string time = ((t.Hours > 0) ? ((t.Hours <= 9) ? string.Format("{0:D1}:{1:D2}:{2:D2}", t.Hours, t.Minutes, t.Seconds) : string.Format("{0:D2}:{1:D2}:{2:D2}", t.Hours, t.Minutes, t.Seconds)) : ((t.Minutes > 0) ? ((t.Minutes <= 9) ? string.Format("{0:D1}:{1:D2}", t.Minutes, t.Seconds) : string.Format("{0:D2}:{1:D2}", t.Minutes, t.Seconds)) : ((t.Seconds <= 9) ? string.Format("{0:D1}", t.Seconds) : string.Format("{0:D2}", t.Seconds))));
			timer.text = time;
			yield return new WaitForSeconds(1f);
		}
		timer.text = "0";
		yield return new WaitForSeconds(1f);
	}
}
